<?
class xmlDB{
	
	//操作表名(xml文件名)
	var $tb;
	
	//配置信息
	var $DBfile;
	var $DBsetup;
	var $DBtpl;
	
	//xml文件头
	var $xmlheader = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
	
	var $listcount;
	var $pagesize = 10;
	
	
	//初始化
	function xmlDB($DBconfig){
		//连接数据库
		$this->SelectDB($DBconfig);
	}
	
	//建立数据库
	function SetupDB($dbtpl = ""){
		if(empty($dbtpl))$dbtpl = $this->DBtpl;
		//生成数据结构字符串
		//$xmlstr = $this->xmlheader."\n<pigdb>\n<pig_tpl>\n".$dbtpl."\n</pig_tpl>\n<pig_data>\n</pig_data>\n</pigdb>";
		$xmlstr = $this->xmlheader."<pigdb><pig_data></pig_data></pigdb>";
		//写入数据结构
		$makexmldb = $this->writeFile($this->DBfile,$xmlstr);
		if($makexmldb === false){}//建立文件错误,抛出异常
	}
	
	function TplFormat(){
		$dbtpl = "";
		$dbtpl .= "<rows id=\"\$id\">";
		preg_match_all('/\{(.+?)\}/s',$this->DBsetup,$rows);
		foreach ($rows[1] as $k=>$v){
			$dbtpl .= "<$v><![CDATA[$$v]]></$v>";
		}
		$dbtpl .= "</rows>";
		return  $dbtpl;
	}
	
	//连接数据库
	function SelectDB($DBconfig = ""){
		//初始化信息
		if(!empty($DBconfig)){
			$this->DBfile = $DBconfig['file'];
			$this->DBsetup = $DBconfig['setup'];
		}
		//生成模板
		$this->DBtpl = $this->TplFormat();
		//验证数据库
		if(!file_exists($this->DBfile)){//如果文件不存在,建立数据库
			$this->SetupDB();
		}
	}
	
	//插入数据
	function Insert($arr){
		//取得数据库数据 $xmlstr
		$xmlstr = $this->readfile();
		//取得数据ID $rows[2][0]
		preg_match_all('/<rows.*id=\"(\d*)\"/s',$xmlstr,$rows);
		$newid = $rows[1][0]+1;
		if(empty($newid))$newid = 1;
		//增量生成数据 $addstr
		$addstr = $this->DBtpl;
		$addstr = preg_replace('/\$id/s',$newid,$addstr);
		foreach ($arr as $k=>$v){
			$addstr = preg_replace('/\$'.$k.'/s',$v,$addstr);
		}
		//组成新数据 $xmlstr
		$xmlstr = preg_replace('/<\/pig_data>/s',$addstr."</pig_data>",$xmlstr);
		//写入数据
		$addnew = $this->writeFile($this->DBfile,$xmlstr);
	}
	
	//读取数据
	function GetList($where = "",$page=1){
		if(empty($page))$page = 1;
		//取得数据库数据 $dbstr
		$dbstr = $this->readfile();
		//按行切分
		preg_match_all('/<rows.+?<\/rows>/s',$dbstr,$xmlrows);
		//dump($xmlrows);
		$xmlrows = $xmlrows[0];
		$rows = array();
		$this->listcount = 0;
		//循环数据
		foreach ($xmlrows as $k=>$v){
			//条件
			if(!empty($where) && !preg_match($where,$v))continue;
			$this->listcount++;
			//分页
			if($page != "all"){
				if(($this->listcount) < (($page-1)*$this->pagesize)+1)continue;
				if(($this->listcount) > ($page*$this->pagesize))continue;
			}
			//取ID
			preg_match_all('/<rows id=\"(\d*)\"/s',$v,$rowtmp['id']);
			$rowid = $rowtmp['id'][1][0];
			$rows[$rowid]['id'] = $rowid;
			//取其他值
			preg_match_all('/<rows id=\"\d*\">(.*?)<\/rows>/s',$v,$rowtmp['fs']);
			preg_match_all("/<(.*?)><\!\[CDATA\[(.*?)\]\]\><\//s",$rowtmp['fs'][1][0],$rowtmp['field']);//
			foreach ($rowtmp['field'][1] as $k=>$v){
				$rows[$rowid][$v] = $rowtmp['field'][2][$k];
			}
		}
		krsort($rows);
		return $rows;
	}
	
	//按ID获取单条数据
	function GetOneByID($id){
		//取全部数据
		$xmlstr = $this->readfile();
		preg_match_all('/<rows id=\"'.$id.'\">.+?<\/rows>/s',$xmlstr,$rowstr);
		//取ID
		//preg_match_all('/<rows id=\"(\d*)\"/s',$rowstr[0][0],$rowtmp['id']);
		$rows['id'] = $id;
		//取其他值
		preg_match_all('/<rows id=\"\d*\">(.*?)<\/rows>/s',$rowstr[0][0],$rowtmp['fs']);
		preg_match_all("/<(.*?)><\!\[CDATA\[(.*?)\]\]\><\//s",$rowtmp['fs'][1][0],$rowtmp['field']);
		foreach ($rowtmp['field'][1] as $k=>$v){
			$rows[$v] = $rowtmp['field'][2][$k];
		}
		return $rows;
	}
	
	//更新数据
	function Update($arr){
		//取全部数据
		$xmlstr = $this->readfile();
		//取数据ID
		$upid = $arr['id'];
		//取数据行
		preg_match_all('/<rows id=\"'.$upid.'\">(.+?)<\/rows>/s',$xmlstr,$rowtmp['fs']);
		$newstr = $rowtmp['fs'][1][0];
		//生成新数据行 $newstr
		foreach ($arr as $k=>$v){
			if($k=="id")continue;
			$newstr = preg_replace('/<'.$k.'>\<\!\[CDATA\[(.*?)\]\]\><\/'.$k.'>/s',"<$k><![CDATA[$v]]></$k>",$newstr);
		}
		//组成新数据 $xmlstr
		$xmlstr = preg_replace('/<rows id=\"'.$upid.'\">(.+?)<\/rows>/s',"<rows id=\"".$upid."\">$newstr</rows>",$xmlstr);
		//写入新数据
		$addnew = $this->writeFile($this->DBfile,$xmlstr);
	}
	
	//删除数据
	function Del($id){
		//清除数据
		$xmlstr = preg_replace('/<rows id=\"'.$id.'\">(.+?)<\/rows>/s',"",$this->readfile());
		//写入新数据
		$addnew = $this->writeFile($this->DBfile,$xmlstr);		
	}
	
	//获取数据库数据
	function GetDB(){
		$xmlstr = $this->readfile();
		preg_match_all('/<pig_data.??>(.+?)<\/pig_data>/s',$xmlstr,$dbstr);
		return $dbstr[1][0];
	}
	
	//读取文件
	function readFile($f = ""){
		if(empty($f)) $f = $this->DBfile;
		if(file_exists($f)){
			return file_get_contents($f);
		}else{
			return false;
		}	
	}
	
	//写入文件
	function writeFile($f,$c){
		if(makeDir(dirname($f))){
			if($fp = fopen($f,"w")){
				@flock($fp,LOCK_EX);
				if(@fwrite($fp,$c)){
					@flock($fp,LOCK_UN);
					fclose($fp);
					return true;
				}else {
					@flock($fp,LOCK_UN);
					fclose($fp);
					return false;
				}
			}
		}	
	}
	
	//创建目录
	function makeDir( $dir, $mode = "0777" ){
		if( ! $dir ) return 0;
		$dir = str_replace( "\\", "/", $dir );
		$mdir = "";
		foreach( explode( "/", $dir ) as $val ) {
			$mdir .= $val."/";
			if( $val == ".." || $val == "." || trim( $val ) == "" ) continue;
			if( ! file_exists( $mdir ) ) {
				if(!@mkdir( $mdir, $mode )){
					return false;
				}
			}
		}
		return true;
	}
}


?>